package weaver.interfaces.ybcn.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.ybcn.duss.utils.T100Util;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-04-28
 * @Description: 采购合同审批流程调用采购核价单生成接口
 * @ModifyLog: OA-T100，OA将采购合同审批（信息记录）传给T100
 **/
public class WeaCghtsplcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        String requestname = requestInfo.getRequestManager().getRequestname();
        writeLog(">>>>>WeaCghtsplcAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            T100Util t100Util = new T100Util();
            rs.execute("select * from " + tablename + " where requestid=?", requestid);
            if (rs.next()) {
                JSONObject payloadJson = new JSONObject();
                JSONObject stddataJson = new JSONObject();
                JSONObject parameterJson = new JSONObject();
                JSONArray masterList = new JSONArray();
                JSONObject masterJson = new JSONObject();
                String mainid = Util.null2String(rs.getString("id"));
                //单别	主表	物料类型
                masterJson.put("pmdidocno", Util.null2String(rs.getString("wllx")));
                String sqrq = Util.null2String(rs.getString("sqrq"));
                //单据日期	主表	申请日期
                masterJson.put("pmdidocdt", t100Util.getTimestamp(sqrq));
                //申请人	主表	申请人
                masterJson.put("pmdi002", Util.null2String(rs.getString("sqr")));
                //供应商代号	主表	供应商
                masterJson.put("pmdi004", Util.null2String(rs.getString("gys1")));
                //标题	主表	标题		系统字段
                masterJson.put("pmdi030", requestname);
                //OA流程归档编码	主表	流程编号
                masterJson.put("source_no", Util.null2String(rs.getString("lcbh")));
                String DATAB = "";
                String DATBI = "";
                JSONArray detailList = new JSONArray();
                rs.execute("select DATAB,DATBI,wlbh,PEINH from " + tablename + "_dt1 where mainid=?", mainid);
                int count = 0;
                while (rs.next()) {
                    JSONObject detailJson = new JSONObject();
                    if (count == 0) {
                        DATAB = Util.null2String(rs.getString("DATAB"));
                        DATBI = Util.null2String(rs.getString("DATBI"));
                    }
                    //料号	明细表	物料编号
                    detailJson.put("pmdj002", Util.null2String(rs.getString("wlbh")));
                    //单价	明细表	单价
                    detailJson.put("pmdj011", Util.null2String(rs.getString("PEINH")));
                    count++;
                    detailList.add(detailJson);
                }
                //生效日期	明细表	有效期从
                masterJson.put("pmdi015", t100Util.getTimestamp(DATAB));
                //失效日期	明细表	有效期至	DATBI
                masterJson.put("pmdi016", t100Util.getTimestamp(DATBI));
                masterJson.put("detail", detailList);
                masterList.add(masterJson);
                parameterJson.put("master", masterList);
                stddataJson.put("parameter", parameterJson);
                payloadJson.put("std_data", stddataJson);
                writeLog(">>>>>WeaCghtsplcAction>>>>>payloadJson=" + payloadJson);
                JSONObject resultJson = t100Util.PriceListCreate(payloadJson, "WeaCghtsplcAction", requestid, src);
                writeLog(">>>>>WeaCghtsplcAction>>>>>resultJson=" + resultJson);
                String message = Util.null2String(resultJson.getString("message"));
                if ("success".equals(message)) {
                    JSONObject dataJson = JSONObject.parseObject(resultJson.getString("data"));
                    JSONObject retPayloadJson = JSONObject.parseObject(dataJson.getString("payload"));
                    JSONObject retStddataJson = JSONObject.parseObject(retPayloadJson.getString("std_data"));
                    JSONObject retParameterJson = JSONObject.parseObject(retStddataJson.getString("parameter"));
                    int failnum = Util.getIntValue(retParameterJson.getString("failnum"), 0);
                    if (failnum > 0) {
                        JSONArray failreturnList = JSONArray.parseArray(retParameterJson.getString("fail_return"));
                        String errorMsg = failreturnList.getJSONObject(0).getString("error_msg");
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + errorMsg);
                        return Action.FAILURE_AND_CONTINUE;
                    } else {
                        JSONArray successreturnList = JSONArray.parseArray(retParameterJson.getString("success_return"));
                        String successMsg = successreturnList.getJSONObject(0).getString("success_msg");
                        rs.execute("update " + tablename + " set erplydh='" + successMsg + "' where requestid=?", requestid);
                    }
                } else {
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaCghtsplcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}